package com.eaglefly.redis.utils;

import org.redisson.Redisson;
import org.redisson.client.codec.Codec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * 功能：Redisson 工具类
 * 时间：2018/1/4 17:34
 *
 * @author zhaojinpeng
 */
public class RedissonUtils {

    private final static Logger logger = LoggerFactory.getLogger(RedissonUtils.class);

    /**
     * 生成真实redisson分布式对象
     *
     * @param redisson
     * @param supperInterface 对象接口类型
     * @param redisKey        redis对应的key值
     * @param codec           序列化方式
     * @param <T>             对象接口类型
     * @return
     */
    public static <T> Object getRedisObject(Redisson redisson, Class<T> supperInterface, String redisKey, Codec codec) {
        //获取对应的方法名
        String simpleName = supperInterface.getSimpleName();
        String methodName = "get" + simpleName.substring(1);

        Object redisObject = null;

        try {
            //初始化redisson对象
            Method method = redisson.getClass().getMethod(methodName, String.class, Codec.class);
            redisObject = (T) method.invoke(redisson, redisKey, codec);
        } catch (Exception e) {
            logger.error("初始化代理实现失败：" + methodName + "运行失败！");
            e.printStackTrace();
        }

        return redisObject;

    }
}
